/*************************************************************************
	> File Name:    main.cpp
	> Author:       陈猛
	> Mail:         cm_lyws@163.com
	> Created Time: 2024年06月27日 星期四 22时50分51秒
	> Description:  
 ************************************************************************/

#include <events/common_log.h>
#include "ssicloud_trip_dashboard.h"
#include "ssicloud_energy_analysis.h"
#include "ssicloud_behaviour_summary.h"
#include "ssicloud_apptrip_report.h"
#include "ssicloud_vs_maintain.h"
#include "ssicloud_vs_eventtrack.h"
#include "ssicloud_vs_feedback.h"

using namespace ssicloud;


class GlobalState : public UINotifyIface
{
public:
    void dataPrepare(int objname, void *obj) {
        GEN_Printf(LOG_DEBUG, "load over.");

        switch (objname) {
        case OBJECT_NAME_ApptripReport:
        {
            ApptripReport *req = reinterpret_cast<ApptripReport *>(obj);
            ApptripNode &mItem = req->item();

            GEN_Printf(LOG_INFO, "ApptripReport[%p] request ok", req);
            GEN_Printf(LOG_INFO, "---------行程分析-------");
            GEN_Printf(LOG_INFO, "分数: %0.02g", mItem.score);
            GEN_Printf(LOG_INFO, "运行时长: %0.02g", mItem.totalTime);
            GEN_Printf(LOG_INFO, "行驶里程: %0.02g", mItem.totalMile);
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_BehaviourSummary:
        {
            BehaviourSummary *req = reinterpret_cast<BehaviourSummary *>(obj);
            ListTable<BehaviourLNode> &mNodes = req->nodes();
            ListTable<BehaviourLNode>::iterator it = mNodes.begin();

            GEN_Printf(LOG_INFO, "BehaviourSummary[%p] request ok", req);
            GEN_Printf(LOG_INFO, "---------驾驶行为---------");
            for (; it != mNodes.end(); ++it)
            {
                GEN_Printf(LOG_DUMP, "type: %s, %s, 次数: %s", it->type.string(), it->name.c_str(), it->cnt.c_str());
            }
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_EnergyAnalysis:
        {
            EnergyAnalysis *req = reinterpret_cast<EnergyAnalysis *>(obj);
            ListTable<EmergyLNode> &mNodes = req->nodes();
            ListTable<EmergyLNode>::iterator it = mNodes.begin();
            GEN_Printf(LOG_INFO, "EnergyAnalysis[%p] request ok", req);

            GEN_Printf(LOG_INFO, "---------能耗分析---------");
            for (; it != mNodes.end(); ++it)
            {
                GEN_Printf(LOG_DUMP, "日期:%s, 能耗:%s, 里程:%s,百公里能耗:%s", it->day.string(), it->fuels.c_str(), it->miles.c_str(), it->kmFuel.string());
            }
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_TripDashboard:
        {
            TripDashboard *req = reinterpret_cast<TripDashboard *>(obj);
            TripNode &mItem = req->item();
            GEN_Printf(LOG_INFO, "TripDashboard[%p] request ok", req);

            GEN_Printf(LOG_INFO, "---------数据总览-------");
            GEN_Printf(LOG_INFO, "能耗: %s", mItem.fuels.c_str());
            GEN_Printf(LOG_INFO, "油耗: %s", mItem.oilFuel.c_str());
            GEN_Printf(LOG_INFO, "气耗: %s", mItem.gasFuel.c_str());
            GEN_Printf(LOG_INFO, "里程: %s", mItem.miles.c_str());
            GEN_Printf(LOG_INFO, "运行时长: %s", mItem.times.c_str());
            GEN_Printf(LOG_INFO, "平均速度: %s", mItem.svgSpeed.c_str());
            GEN_Printf(LOG_INFO, "平均能耗: %s", mItem.kmFuels.c_str());
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_VSEventTrack:
        {
            VSEventTrack *req = reinterpret_cast<VSEventTrack *>(obj);
            GEN_Printf(LOG_INFO, "VSEventTrack[%p] request ok", req);
            break;
        }
        case OBJECT_NAME_VSFeedback:
        {
            VSFeedback *req = reinterpret_cast<VSFeedback *>(obj);
            GEN_Printf(LOG_INFO, "VSFeedback[%p] request ok", req);
            break;
        }
        case OBJECT_NAME_VSMaintain:
        {
            VSMaintain *req = reinterpret_cast<VSMaintain *>(obj);
            MaintainNode &mItem = req->item();

            GEN_Printf(LOG_INFO, "VSMaintain[%p] request ok", req);
            GEN_Printf(LOG_INFO, "---------维保服务 获取里程信息-------");
            GEN_Printf(LOG_INFO, "vin: %s", mItem.vin.c_str());
            GEN_Printf(LOG_INFO, "设置里程: %d", mItem.additionalMile);
            GEN_Printf(LOG_INFO, "距离下次保养里程: %0.02g", mItem.remainMile);
            GEN_Printf(LOG_INFO, "当前里程: %0.02g", mItem.currentMile);
            GEN_Printf(LOG_INFO, "总行使里程: %0.02g", mItem.maintainMile);
            GEN_Printf(LOG_INFO, "---------完成-------");
            break;
        }
        case OBJECT_NAME_VSMaintainSet:
        {
            VSMaintainSet *req = reinterpret_cast<VSMaintainSet *>(obj);
            GEN_Printf(LOG_INFO, "VSMaintainSet[%p] request ok", req);
            break;
        }
        default:
            GEN_Printf(LOG_INFO, "unkown object");
            break;
        }



        ++ok_times;
    }
    void errorInfo(int objname, void *obj, int type, const ByteString &str)
    {
        GEN_Printf(LOG_INFO, "obj[%d] = %p request failed", objname, obj);
        GEN_Printf(LOG_ERROR, "load error, type=%d, err: %s.", type, str.c_str());
        ++err_times;
    }
    int totals() {return ok_times + err_times;}

    int ok_times = 0;
    int err_times = 0;
};

int main(int /*argc*/, char **/*argv*/)
{
    static GlobalState state;
    int total_request = 0;

    if (1){
        // 数据总览
        static TripDashboard req;
        // 设置接口状态通知
        req.setUINotify(&state);
        // 开始请求，并获取数据
        req.requestAll();
        ++total_request;
    }

    if (0){
        // 能耗数据下载
        static EnergyAnalysis req;
        req.setUINotify(&state);
        // 开始请求，并获取数据
        req.requestAll();
        ++total_request;
    }

    if (0) {
        // 驾驶行为
        static BehaviourSummary req;
        req.setUINotify(&state);
        // 开始请求，并获取数据
        req.requestAll();
        ++total_request;
    }

    if (0) {
        // 行程分析
        static ApptripReport req;
        req.setUINotify(&state);
        // 开始请求，并获取数据
        req.requestAll();
        ++total_request;
    }

    if (0) {
        // 维保服务
        static VSMaintain req;
        req.setUINotify(&state);
        // 开始请求，并获取数据
        req.requestAll();
        ++total_request;

        //static VSMaintainSet reqset;
        //reqset.setUINotify(&state);
        //reqset.requestSetMile();
        //++total_request;
    }

    if (0){
        // 车机埋点, 上传
        EventTrackIn in;
        in.eventId = "1";
        in.eventParam = "0";
        in.viewNum = 0;
        in.viewSuccessNum = 1;
        in.body = "{}";

        static VSEventTrack req;
        req.setUINotify(&state);
        // 开始请求，并上传数据
        req.postEventInfo(in);
        ++total_request;
    }

    if (0) {
        // 上传文字信息
        {
            FeedbackIn in;
            in.fname = "ChenMeng";
            in.fphone = "88888888";
            in.type = 2; // 文字信息
            in.feedbackType = 1; // 反馈类型共分为: 1:质量反馈 2:服务反馈 3:投诉 4:建议 5:其它
            in.content = "{}";
            in.uploadTime = "2024-06-1210:14:00";

            static VSFeedback req;
            req.setUINotify(&state);
            // 开始请求，上传数据
            req.postEventInfo(in);
            ++total_request;
        }

        // 传入音频文件
        if (0){
            FeedbackIn in;
            in.fname = "test";
            in.fphone = "88888888";
            in.type = 1; // 语音文件
            in.feedbackType = 1; // 反馈类型共分为: 1:质量反馈 2:服务反馈 3:投诉 4:建议 5:其它
            in.file = "/tmp/clash-verge-check.yaml";
            in.uploadTime = "2024-06-1210:14:00";
            static VSFeedback req;
            req.setUINotify(&state);
            // 开始请求，上传数据
            req.postEventInfo(in);
            ++total_request;
        }
    }

    while (state.totals() != total_request)
    {
        sleep(1);
    }

    GEN_Printf(LOG_DEBUG, "request over.");

    return 0;
}


